#include<iostream>

#include<vector>
#include<unordered_map>


using namespace std;




int findMaxLength(vector<int>& nums) {
    for (int i = 0; i < nums.size(); i++)
    {
        if (nums[i] == 0) nums[i] = -1;
    }

    unordered_map<int, int> hash;
    int sum = 0, ret = 0;
    hash[0] = -1;
    for (int i = 0; i < nums.size(); i++)
    {
        sum += nums[i];
        if (hash.count(sum)) ret = i - hash[sum];
        if (!hash.count(sum)) hash[sum] = i;
    }
    return ret;

}

int main()
{
    vector<int> v = { 0,1,1,0,1,1,1,0 };
    cout << findMaxLength(v) << endl;
    return 0;
}